package com.ton.common.controller.task;

import com.ton.common.controller.task.req.CreateJobReq;
import com.ton.context.QuartzContext;
import com.ton.system.dto.R;
import com.ton.utils.quartz.QuartzUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/quartz/")
@Api(value = "定时任务管理",tags = "定时任务管理")
public class QuartzController {
    //注入任务调度
    @Autowired
    private Scheduler scheduler;

    @PostMapping("/createJob")
    @ResponseBody
    @ApiOperation(value = "创建定时任务",notes = "创建定时任务")
    public String  createJob(CreateJobReq createJobReq)  {
        try {
            //QuartzBean
            //进行测试所以写死
            createJobReq.setJobClass("com.tron.task.MyTask1");
            createJobReq.setJobName("test1");
            createJobReq.setCronExpression("*/10 * * * * ?");

            QuartzContext quartzContext = new QuartzContext();
            quartzContext.setJobName(createJobReq.getJobName());
            quartzContext.setCronExpression(createJobReq.getCronExpression());
            quartzContext.setJobClass(createJobReq.getJobClass());

            Map<String,Object> map = new HashMap<>();
            map.put("key",createJobReq.getKey());
            quartzContext.setData(map);
            QuartzUtils.createScheduleJob(scheduler, quartzContext);
        } catch (Exception e) {
            return "创建失败";
        }
        return "创建成功";
    }

    @GetMapping("/createOrderJob")
    @ResponseBody
    @ApiOperation(value = "创建订单定时任务",notes = "创建订单定时任务")
    public R<?> createOrderJob(@RequestParam(value = "amount") Integer amount){
        String account = "TDVbYKBu5kY8BmxWvzmYizswf8UVJLy2mN";
        String privateKey = "b91990d47a289b8524706ea0578f559d3f08bd0f7de02df4956075e66503f048";
        amount = 500;

        QuartzContext quartzContext = new QuartzContext();
        quartzContext.setJobName(account);
        quartzContext.setCronExpression("*/10 * * * * ?");
        quartzContext.setJobClass("com.tron.task.tron.OrderListener");

        Map<String,Object> map = new HashMap<>();
        map.put("account",account);
        map.put("privateKey",privateKey);
        map.put("amount",amount);
        quartzContext.setData(map);
        QuartzUtils.createScheduleJob(scheduler, quartzContext);
        return R.ok("订单创建成功");
    }


    @PostMapping("/pauseJob")
    @ResponseBody
    @ApiOperation(value = "暂停定时任务",notes = "暂停定时任务")
    public String  pauseJob()  {
        try {
            QuartzUtils.pauseScheduleJob (scheduler,"test1");
        } catch (Exception e) {
            return "暂停失败";
        }
        return "暂停成功";
    }

    @PostMapping("/runOnce")
    @ResponseBody
    @ApiOperation(value = "运行定时任务",notes = "运行定时任务")
    public String  runOnce()  {
        try {
            QuartzUtils.runOnce (scheduler,"test1");
        } catch (Exception e) {
            return "运行一次失败";
        }
        return "运行一次成功";
    }

    @PostMapping("/resume")
    @ResponseBody
    @ApiOperation(value = "启动定时任务",notes = "启动定时任务")
    public String  resume()  {
        try {
            QuartzUtils.resumeScheduleJob(scheduler,"test1");
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改定时任务",notes = "修改定时任务")
    public String  update(QuartzContext quartzContext)  {
        try {
            //进行测试所以写死
            quartzContext.setJobClass("com.tron.task.MyTask1");
            quartzContext.setJobName("test1");
            quartzContext.setCronExpression("10 * * * * ?");
            QuartzUtils.updateScheduleJob(scheduler, quartzContext);
        } catch (Exception e) {
            return "启动失败";
        }
        return "启动成功";
    }
}
